<template>
	<form :action="action" :target="target" :method="type"  enctype="enctype" ref="formBlock">
		<slot></slot>
		<verify ref="verifyFrom"></verify>
	</form>
</template>

<script>
	import verifyFrom from "./verify.vue";
	/*
	组件属性[
		action:表单提交地址
		target:表单提交窗口
		type:提交方式 get/post
		isupfile:是否上传文件 false(默认)/true
	]
	组件事件[
		submit:选项发生改变时回调的父组件方法，callback(obj[当前分数])
	]
	*/

	export default{
		props:{
			'action':{type:String,defalut:""},
			'target':{type:String,default:""},
			'type':{type:String,default:"get"},
			'isupfile':{type:Boolean,default:false}//同步上传时应设置为true,异步上传此参数无效
		},
		data(){
			return {
				uiType:"ui-form",
				starArrRecord:[],
				formData:{}
			}
		},
		methods:{			
			submit(){
				this.formData=this.gather();
                let res=this.verify(this.formData);
                if(res==true){
					this.$emit('submit',this.formData);
                }
			},
			gather(){
				let formData={};
				for(let i=0;i<this.$children.length;i++){
				    let o=this.$children[i];
                    if(o.uiType){
                        if(o.uiType=='ui-form-item'){
                            for(let k in o.child){
                                formData[k]=o.child[k];
							}
						}else{
                            formData[o.name]=o;
                        }
					}
                }
				return formData;
			},
			verify(data){
			    let res=this.$refs.verifyFrom.verify(data);
			    if(res!=true){
                    for(let i in res.pass){
                        let o=res.pass[i];
                        if(o.$parent.uiType && o.$parent.uiType=="ui-form-item"){
                            o.$parent.error='';
                        }
                    }
                    for(let i in res.notPass){
                        let o=res.notPass[i];
                        if(o.$parent.uiType && o.$parent.uiType=="ui-form-item"){
                            console.log(o.resText)
                            o.$parent.error=o.resText;
						}
					}
					return false;
				}else{
                    for(let i in this.$children){
                        let o=this.$children[i];
                        if(o.uiType && o.uiType=="ui-form-item"){
                            o.error='';
                        }
                    }
			 		return true;
				}
			}
		},
		computed:{
			enctype(){
				let str="application/x-www-form-urlencoded";
				if(this.isupfile){
					str="multipart/form-data";
				}
				return str;
			}
		},
		components:{
            "verify":verifyFrom
		}
	}
</script>
